package compiler.tac;

import compiler.tac.Instruction.Operator;


public class SimpleExpt {
	//String name;
	Operator oprt;
	String opl;
	String opr;
	
	static int count = 0;
	
	public SimpleExpt(Operator oprt, String opl, String opr)
	{
		this.oprt = oprt;
		//this.name = n;
		
		this.opl = cutName(opl);
		this.opr = cutName(opr);
	}
	public SimpleExpt()
	{
		this.oprt = null;
		//this.name = null;
		this.opl = null;
		this.opr = null;
	}
	
	public static String getname()
	{
		count++;
		return "$E" + (count-1);
	}
	
	public String toString()
	{
		return opl+oprt+opr;
	}
	public static String cutName(String name)
	{
		String newname = name;
		int size = name.length()-1;
		//this is a variable
		if(size != -1 && Character.isDigit(name.charAt(size)))
		{	
			while(size != -1 && Character.isDigit(name.charAt(size)))
				size --;
			if(size !=-1 && name.charAt(size)=='_')
				newname = name.substring(0, size);
			else 
				newname = name;
		}
		//this is an expression
		if(size != -1 && name.charAt(size)==']')
		{
			int position = name.indexOf("[");
			if(position != -1)
				return name.substring(0, position);
			else
				return name;
		}	
		return newname;
	}
	
	public static int getVersion(String name)
	{
		int position = name.indexOf("_");
		if(position!=-1)
			return Integer.parseInt(name.substring(position+1, name.length()));
		position =name.indexOf("[");
		if(position!=-1)
			return Integer.parseInt(name.substring(position+1, name.length()-1));
		
		return -1;
	}
	public boolean equals(SimpleExpt e)
	{
		if(this.opl.equals(e.opl) && this.opr.equals(e.opr) && this.oprt.equals(e.oprt))
			return true;
		return false;
	}
}
